package LeetCode;



import java.math.BigInteger;

public class No02 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        while (l1!=null){
            sb1.append(l1.val);
            l1 = l1.next;
        }
        while (l2!=null){
            sb2.append(l2.val);
            l2 = l2.next;
        }
        BigInteger num1 = new BigInteger(sb1.reverse().toString());
        BigInteger num2 = new BigInteger(sb2.reverse().toString());
        BigInteger num3 = num1.add(num2);
        sb1 = new StringBuilder(num3.toString());
        String s = sb1.reverse().toString();
        ListNode head = new ListNode(-1);
        ListNode curr = head;
        for (int i = 0; i < s.length(); i++) {
            curr.next = new ListNode(s.charAt(i)-'0');
            curr = curr.next;
        }
        return head.next;

//        ListNode head = new ListNode((l1.val+l2.val)%10);
//        int num = (l1.val+l2.val)/10;
//        l1 = l1.next;
//        l2 = l2.next;
//        ListNode curr = head;
//        while(l1!=null||l2!=null){
//            if(l1==null){
//                curr.next = new ListNode((l2.val+num)%10);
//                num = (l2.val+num)/10;
//                l2 = l2.next;
//            }else if(l2==null){
//                curr.next = new ListNode((l1.val+num)%10);
//                num = (l1.val+num)/10;
//                l1 = l1.next;
//            }else{
//                curr.next = new ListNode((l1.val+l2.val+num)%10);
//                num = (l1.val+l2.val+num)/10;
//                l1 = l1.next;
//                l2 = l2.next;
//            }
//            curr = curr.next;
//        }
//        if(num!=0) curr.next = new ListNode(num);
//        return head;
    }
}
